#include "screen.h"

/** 
 * @brief u8g2数据类型定义
 * 
 */
u8g2_t CAST_OLED;

/** 
 * @brief u8g2接口与显示函数
 * @param u8x8 
 * @param msg 
 * @param arg_int 
 * @param arg_ptr 
 * @return 不重要
 */ 
uint8_t u8x8_stm32_gpio_and_delay(U8X8_UNUSED u8x8_t *u8x8,
                                  U8X8_UNUSED uint8_t msg, U8X8_UNUSED uint8_t arg_int,
                                  U8X8_UNUSED void *arg_ptr)
{
  switch (msg)
  {
  case U8X8_MSG_GPIO_AND_DELAY_INIT:
    HAL_Delay(1);
    break;
  case U8X8_MSG_DELAY_MILLI:
    HAL_Delay(arg_int);
    break;
  case U8X8_MSG_GPIO_DC:
    HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, arg_int);
    break;
  case U8X8_MSG_GPIO_RESET:
    HAL_GPIO_WritePin(OLED_RES_GPIO_Port, OLED_RES_Pin, arg_int);
    break;
  }
  return 1;
}

/** 
 * @brief u8g2写入函数
 * @param u8x8 
 * @param msg 
 * @param arg_int 
 * @param arg_ptr 
 * @return 不重要
 */ 
uint8_t u8x8_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
                               void *arg_ptr)
{
  switch (msg)
  {
  case U8X8_MSG_BYTE_SEND:
    HAL_SPI_Transmit(&hspi1, (uint8_t *)arg_ptr, arg_int, 10000);
    break;
  case U8X8_MSG_BYTE_INIT:
    break;
  case U8X8_MSG_BYTE_SET_DC:
    HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, arg_int);
    break;
  case U8X8_MSG_BYTE_START_TRANSFER:
    break;
  case U8X8_MSG_BYTE_END_TRANSFER:
    break;
  default:
    return 0;
  }
  return 1;
}

/**
 * @brief oled屏幕初始化以及清屏
 * @retval None
 */
void oledInit()
{
  u8g2_Setup_ssd1306_128x64_noname_f(&CAST_OLED, U8G2_R0, u8x8_byte_4wire_hw_spi, u8x8_stm32_gpio_and_delay);
  u8g2_InitDisplay(&CAST_OLED);
  u8g2_SetPowerSave(&CAST_OLED, 0);
  u8g2_SetFont(&CAST_OLED, u8g2_font_profont12_mr); 
  u8g2_FirstPage(&CAST_OLED);
  do
  {

  } while (u8g2_NextPage(&CAST_OLED));
}

